# -*- coding: utf-8 -*-
# @Time    : 2019/10/23 10:57
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : thread_queue.py
# 线程间通讯
# 模拟爬虫， 获取文章列表页及获取文章详情页的并发


# 1. 通过Thread类实例化
import time
import threading
from advanced.chapter10 import variables  # 全局变量独立存储 不能直接导入变量必须以.的形式引入变量


# detail_url_list = []
total = 0

def get_detail_html():
    """ 获取文章详情页 """
    detail_url_list = variables.detail_url_list
    while True:
        if len(detail_url_list):
            t_time = time.time()
            url = detail_url_list.pop()  # 线程不安全
            # for url in detail_url_list:
            print("get detail html started\r\n")
            time.sleep(1)
            print(url)
            time.sleep(1)

            print("get detail html end\r\n")
            time.sleep(2)

            print(f" threading last time {time.time() - t_time}\r\n")
            global total
            total += 1
        if total == 20:
            break


def get_detail_url():
    """ 获取文章列表页 """
    detail_url_list = variables.detail_url_list
    while True:
        s_time = time.time()
        print("get detail url started\r\n")
        time.sleep(1)

        for i in range(20):
            detail_url_list.append(f"http://projects.edu.com/{i}\r\n")
        print(f"threading last time {time.time() - s_time}\r\n")
        print("get detail url end\r\n")
        break


# 实现线程间通讯的方法
# 1. 共享变量: 声明全局变量，在各线程中使用 存在问题：线程操作不安全

if __name__ == '__main__':

    thread_detail_url = threading.Thread(target=get_detail_url)  # threading类实例化只能传递函数名，参数使用tuple传递
    start_time = time.time()
    threads = []
    for i in range(10):

        html_thread = threading.Thread(target=get_detail_html)
        html_thread.start()
        threads.append(html_thread)

    thread_detail_url.start()
    thread_detail_url.join()
    for j in threads:
        j.join()

    print(f"total last time {time.time() - start_time}\r\n")
    # 时间为0
    # 存在三个线程 两个创建的线程以及一个记录时间的主线程
    # 引申的需求，当主线程退出时，子线程kill掉  使用setDaemon 设置子线程为守护线程
